package Groep3;

import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Scanner;


public class Client {
	
	private Socket clientSocket;
	private BufferedReader inFromUser;
	private BufferedReader inFromServer;
	private DataOutputStream outToServer;
	
	public static void main (String argv[]) throws Exception {
		try
		{
			new Client();
		}
		catch( Exception e)
		{
			System.out.println(e);
		}
	}
	
	public Client() throws Exception {
		run();
	}
	
	public void run() throws Exception {
		
		while(true) {
		
			inFromUser = new BufferedReader(new InputStreamReader(System.in));
			
			
			String getThis;
			Scanner in = new Scanner(System.in);
			
			System.out.println("Voer url in:");
			getThis = in.nextLine();
		
		
			if(getThis.substring(0,3).equalsIgnoreCase("GET") || getThis.substring(0,4).equalsIgnoreCase("HEAD")) {
				clientSocket = new Socket("localhost", 6789);
				outToServer = new DataOutputStream(clientSocket.getOutputStream());
				inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
				getAndHead(getThis);
				clientSocket.close();
				outToServer.close();
				inFromServer.close();
			}

			else if(getThis.substring(0,4).equalsIgnoreCase("POST")) {
				clientSocket = new Socket("localhost", 6789);
				outToServer = new DataOutputStream(clientSocket.getOutputStream());
				inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
				post(in.nextLine());
				clientSocket.close();
				outToServer.close();
				inFromServer.close();
			}
			
			else {
				getThis = null;
				System.out.println("Error");
			}
		}
		
	}
	
	public void getAndHead(String getThis) throws Exception {
		String website;
		String thisLine;
			outToServer.writeBytes(getThis + " HTTP/1.1" + '\n');
		while((thisLine = inFromServer.readLine()) != null) {
			System.out.println("From server: " + thisLine);
		}
			
	}
	
	public void post(String getThis) {
		
	}
	
	
}
